﻿// Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
using System;
using System.IO;
using System.Net;
using System.Text;
using ForgeCraft.Network.Handlers;

namespace ForgeCraft.Network
{
    public class PacketHandlers
    {
        private static PacketHandler[] handlers = new PacketHandler[256];

        public static void Init()
        {

            for (int i = 0; i < handlers.Length; ++i)
                handlers[i] = null;

            handlers[(int)PacketType.Handshake] = new HandshakeHandler();
            handlers[(int)PacketType.LoginDetails] = new LoginHandler();
            handlers[(int)PacketType.Message] = new ChatHandler();
            handlers[(int)PacketType.Disconnect] = new DisconnectHandler();
            handlers[(int)PacketType.PlayerPosition] = new MovementHandler();
            handlers[(int)PacketType.PlayerPositionLook] = new MoveLookHandler();
            handlers[(int)PacketType.PlayerLook] = new LookHandler();
            handlers[(int)PacketType.Player] = new BoolHandler();
            handlers[(int)PacketType.PlayerHolding] = new HoldingHandler();
            handlers[(int)PacketType.Animation] = new ArmAnimationHandler();
            handlers[(int)PacketType.PlayerDigging] = new BlockDigHandler();
            handlers[(int)PacketType.PlayerBlockPlace] = new BlockPlaceHandler();
            handlers[(int)PacketType.InteractEntity] = new InteractionHandler();

        }

        public static void HandlePacket(BinaryReader reader, Session session, Player player, byte opcode, byte lastopcode)
        {
            //Logger.Log("In: " + opcode, LogType.Debug);

            if (opcode > handlers.Length || handlers[opcode] == null)
            {
                if (opcode != 0)
                    Logger.Log("Unhandled opcode: {0} Last opcode: {1}", LogType.Debug, opcode, lastopcode);

                return;
            }

            handlers[opcode].Handle(reader, session, player);

        }

    }
}
